﻿using System;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.Audio;

public class AudioManager : Singleton<AudioManager>
{
    [CompilerGenerated]
    private static Func<float> <>f__am$cache3;
    private AudioMixer mixer;
    private AudioSource musicSource;
    public const string MusicVolumeKey = "MusicVolume";
    public const float normalVolume = 0f;
    public const float silenceVolume = -80f;
    private AudioSource soundSourcePrefab;
    public const string SoundVolumeKey = "SoundVolume";

    private void Awake()
    {
        this.soundSourcePrefab = new GameObject("AudioSource").AddComponent<AudioSource>();
        this.soundSourcePrefab.transform.parent = base.transform;
        this.musicSource = new GameObject("AudioSource").AddComponent<AudioSource>();
        this.musicSource.loop = true;
        this.musicSource.transform.parent = base.transform;
        this.mixer = Resources.Load<AudioMixer>("AudioMixer");
        if (this.mixer != null)
        {
            this.soundSourcePrefab.outputAudioMixerGroup = this.mixer.FindMatchingGroups("Sound").FirstOrDefault<AudioMixerGroup>();
            this.musicSource.outputAudioMixerGroup = this.mixer.FindMatchingGroups("Music").FirstOrDefault<AudioMixerGroup>();
        }
    }

    public void FadeoutMusic(float duration = 10f)
    {
        if (<>f__am$cache3 == null)
        {
            <>f__am$cache3 = () => Time.unscaledDeltaTime;
        }
        this.StartAnimateFloat(1f, 0f, duration, <>f__am$cache3, (Action<float>) (v => (this.musicSource.volume = v)), () => this.musicSource.Stop());
    }

    private float GetVolume(string key)
    {
        return PlayerPrefs.GetFloat(key);
    }

    [RuntimeInitializeOnLoadMethod]
    private static void InitOnLoad()
    {
        Singleton<AudioManager>.Instance.enabled = true;
    }

    public void PlaybackMusic(AudioClip clip, bool loop = true)
    {
        this.musicSource.volume = 0.75f;
        this.musicSource.loop = loop;
        this.musicSource.clip = clip;
        this.musicSource.Play();
    }

    public void PlayMusic(AudioClip clip, bool loop = true)
    {
        this.musicSource.loop = loop;
        if ((this.musicSource.clip != clip) || !this.musicSource.isPlaying)
        {
            this.PlaybackMusic(clip, true);
        }
    }

    public void PlaySound(AudioClip clip)
    {
        this.PlaySound(clip, Vector3.zero, true);
    }

    public void PlaySound(AudioClip clip, Vector3 position)
    {
        this.PlaySound(clip, position, false);
    }

    private void PlaySound(AudioClip clip, Vector3 position, bool is2D)
    {
        <PlaySound>c__AnonStorey67 storey = new <PlaySound>c__AnonStorey67 {
            ins = this.soundSourcePrefab.Spawn<AudioSource>(position)
        };
        storey.ins.clip = clip;
        storey.ins.spatialBlend = !is2D ? ((float) 1) : ((float) 0);
        storey.ins.Play();
        storey.ins.transform.parent = base.transform;
        this.StartWaitForSeconds(storey.ins.clip.length, new Action(storey.<>m__F3));
    }

    public static void RegisterSoundSource(AudioSource source)
    {
        if (source != null)
        {
            source.outputAudioMixerGroup = Singleton<AudioManager>.Instance.soundSourcePrefab.outputAudioMixerGroup;
        }
    }

    private void SetVolume(string key, float value)
    {
        PlayerPrefs.SetFloat(key, value);
        this.mixer.SetFloat(key, value);
    }

    private void Start()
    {
        if (this.mixer != null)
        {
            string[] textArray1 = new string[] { "MusicVolume", "SoundVolume" };
            foreach (string str in textArray1)
            {
                if (!PlayerPrefs.HasKey(str))
                {
                    PlayerPrefs.SetFloat(str, 0f);
                }
                this.mixer.SetFloat(str, PlayerPrefs.GetFloat(str));
            }
        }
    }

    public void StopMusic()
    {
        this.musicSource.Stop();
    }

    public AudioClip currentMusicClip
    {
        get
        {
            return this.musicSource.clip;
        }
    }

    public float musicVolume
    {
        get
        {
            return PlayerPrefs.GetFloat("MusicVolume");
        }
        set
        {
            this.SetVolume("MusicVolume", value);
        }
    }

    public float soundVolume
    {
        get
        {
            return this.GetVolume("SoundVolume");
        }
        set
        {
            this.SetVolume("SoundVolume", value);
        }
    }

    [CompilerGenerated]
    private sealed class <PlaySound>c__AnonStorey67
    {
        internal AudioSource ins;

        internal void <>m__F3()
        {
            this.ins.Recycle<AudioSource>();
        }
    }
}

